import core.yt_helper

class Shuffler:

	def __init__(self,playlist_mgr):
		self.playlist_mgr = playlist_mgr
	
	def shuffle(self):
		videos = self.playlist_mgr.get_videos()
		ids_permutation = self._generate_permutation_of_ids(videos)
		
		position = len(videos) - 1
		while position >= 0:
			video_id = ids_permutation[position]
			
			playlist_position = position + 1 #YT playlist position indices are 1-based
			self.playlist_mgr.move_track_to_position(video_id,playlist_position)
			position -= 1
		
	def _generate_permutation_of_ids(self,videos):
		permutation = [ yt_helper.get_id_for_feed_entry(video) for video in videos]
		random.shuffle(permutation)
		return permutation